package com.example.httpclient.Adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.example.httpclient.Crawler.HtmlTagAttr;
import com.example.httpclient.Crawler.HtmlTagHandler;
import com.example.httpclient.Crawler.ImageUrl;
import com.example.httpclient.Crawler.download;
import com.example.httpclient.Crawler.getImageUrlHandler;
import com.example.httpclient.FileUtils.rwFile;
import com.example.httpclient.R;
import com.google.android.material.textfield.TextInputEditText;

import java.util.ArrayList;

public class BottomPopWindows extends PopupWindow {
    private static ArrayList<String> DataList;
    private static ArrayList<String> tempUrls;
    private Context context;
    private int style;
    private static Button btn,btn_load;
    private Handler mHandler;
    private static TextInputEditText editTextWeb,editTextTag,editTextAttr,editTextFilter,editTextDir,editTextSplit,editTextAdd;
    private static ArrayList<String> visitUrls;
    private static String indexUrl="";
    private Boolean isGoingDownload=true;


    /***
     *
     * @param context
     * @param style 0|1
     */
    public BottomPopWindows(Context context, int style, Handler mHandler) {
       this.context=context;
       this.mHandler=mHandler;
       if(style!=1&&style!=0){
           Message message=new Message();
           message.what=0x00;
           message.obj="style类型出错";
           mHandler.sendMessage(message);
           return;
       }
        this.style=style;
    }
    public void setHrefUrl(ArrayList<String> visitUrl){
        this.visitUrls=visitUrl;
    }
    public void setStopDown(boolean isGoingDownload){
        this.isGoingDownload=isGoingDownload;
    }

    private void BindView(){
        View viewContent=View.inflate(context,R.layout.layout_popwindows,null);
        LinearLayout layoutFindView=viewContent.findViewById(R.id.findView);
        LinearLayout layoutDownView=viewContent.findViewById(R.id.downloadView);
        ArrayList<String> stringNames=new ArrayList<>();
        ArrayList<String> webData=new ArrayList<>();
        switch (style){
            case 0:
                layoutFindView.setVisibility(View.VISIBLE);
                layoutDownView.setVisibility(View.GONE);
                editTextWeb=viewContent.findViewById(R.id.txt_web);
                editTextTag=viewContent.findViewById(R.id.txt_label);
                editTextAttr=viewContent.findViewById(R.id.txt_attr);
                editTextFilter=viewContent.findViewById(R.id.txt_filter);
                break;
            case 1:
                layoutFindView.setVisibility(View.GONE);
                layoutDownView.setVisibility(View.VISIBLE);
                editTextAttr=viewContent.findViewById(R.id.down_attr);
                editTextSplit=viewContent.findViewById(R.id.down_split);
                editTextAdd=viewContent.findViewById(R.id.down_addString);
                editTextDir=viewContent.findViewById(R.id.down_dirName);
        }
        setContentView(viewContent);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(1000);
        setAnimationStyle(R.style.Animation_Design_BottomSheetDialog);
        setFocusable(true);
        showAtLocation(viewContent, Gravity.BOTTOM,0,0);
        btn=viewContent.findViewById(R.id.pop_ok);
        btn_load=viewContent.findViewById(R.id.pop_load);
    }
    public void PutDataList(){
        BindView();
        myOnclick myOnclick=new myOnclick();
        btn.setOnClickListener(myOnclick);
        btn_load.setOnClickListener(myOnclick);

    }
    private void showFind(ArrayList<String> DataList ){
        if (DataList.get(0).length() != 0 && !DataList.get(0).equals("") && DataList.get(0)!= null) {
            HtmlTagAttr htmlTagAttr = new HtmlTagAttr("https://" + DataList.get(0), DataList.get(1),
                    DataList.get(2), "" + DataList.get(3), mHandler);
            htmlTagAttr.setHtmlTagHandler(new HtmlTagHandler() {
                @Override
                public void TagHandler(ArrayList<String> TagLinks) {
                    Message message = new Message();
                    message.what = 0x123;
                    message.obj = TagLinks;
                    mHandler.sendMessage(message);
                }
            });
            new Thread(htmlTagAttr).start();
        } else {
            Message message = new Message();
            message.what = 0x00;
            message.obj = "网址不可以为空";
            mHandler.sendMessage(message);
        }
    }
    private class myOnclick implements View.OnClickListener{
        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.pop_load://读取
                    switch (style) {
                        case 0:
                            tempUrls = new ArrayList<>();
                            tempUrls = rwFile.readTxt(context, "findtext.txt");
                            for (int i=0;i<tempUrls.size();i++){
                                switch (i){
                                    case 0:
                                        editTextWeb.setText(tempUrls.get(0));
                                        break;
                                    case 1:
                                        editTextTag.setText(tempUrls.get(1));
                                        break;
                                        case 2:
                                        editTextAttr.setText(tempUrls.get(2));
                                        break;
                                    case 3:
                                        editTextFilter.setText(tempUrls.get(3));
                                        break;
                                }
                            }
                            break;
                        case 1:
                            tempUrls = new ArrayList<>();
                            tempUrls = rwFile.readTxt(context, "downtext.txt");
                            for (int i=0;i<tempUrls.size();i++){
                                switch (i){
                                    case 0:
                                        editTextAttr.setText(tempUrls.get(0));
                                        break;
                                    case 1:
                                        editTextSplit.setText(tempUrls.get(1));
                                        break;
                                    case 2:
                                        editTextAdd.setText(tempUrls.get(2));
                                        break;
                                    case 3:
                                        editTextDir.setText(tempUrls.get(3));
                                        break;
                                }
                            }
                            break;
                    }
                    break;
                case R.id.pop_ok://btn
                    dismiss();
                    switch (style){
                        case 0://查询
                            DataList=new ArrayList<>();
                            DataList.add(String.valueOf(editTextWeb.getText()));
                            if(String.valueOf(editTextWeb.getText()).length()!=0){
                                indexUrl=String.valueOf(editTextWeb.getText());
                            }
                            DataList.add(String.valueOf(editTextTag.getText()));
                            DataList.add(String.valueOf(editTextAttr.getText()));
                            DataList.add(String.valueOf(editTextFilter.getText()));
                            showFind(DataList);
                            break;
                        case 1://下载
                            DataList=new ArrayList<>();
                            DataList.add(String.valueOf(editTextAttr.getText()));
                            DataList.add(String.valueOf(editTextSplit.getText()));
                            DataList.add(String.valueOf(editTextAdd.getText()));
                            DataList.add(String.valueOf(editTextDir.getText()));
                            ImageUrl imageUrl=new ImageUrl(visitUrls, indexUrl, DataList, new getImageUrlHandler() {
                                @Override
                                public void getUrlHandler(ArrayList<String> urls) {
                                    for(String url:urls){
                                        download download=new download(url,mHandler);
                                        if(isGoingDownload){
                                            new Thread(download).start();
                                        }
                                    }
                                }
                            },mHandler);
                            new Thread(imageUrl).start();
                            break;
                    }
                    break;
            }
        }
    }
}
